<template>
  <div style="padding: 10px">
    <div>
      <el-button type="primary"   stripe  @click="add">新增</el-button>
<!--      <el-button type="primary">导入</el-button>-->
<!--      <el-button type="primary">导出</el-button>-->
    </div>
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable></el-input>
      <el-button type="primary" style="margin: 10px" @click="load" :icon="search">查询</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed prop="id" sortable label="ID" width="150"/>
      <el-table-column label="封面" width="120">
        <template #default="scope">
          <el-image
              style="width: 100px;height: 100px"
              :src="scope.row.cover"
              :preview-src-list="[scope.row.cover]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="carPartsName" label="汽车配件名称" width="120"/>
      <el-table-column prop="standard" label="规格" width="120"/>
      <el-table-column prop="carNumber" label="配件编号" width="120"/>
      <el-table-column prop="buy_price" label="进价" width="120"/>
      <el-table-column prop="sale_price" label="卖价" width="120"/>
      <el-table-column prop="number" label="剩余数量" width="120"/>
      <el-table-column prop="sub_number" label="缺货数量" width="120"/>

      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除?" @confirm="handleDelete(scope.row)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin:10px 0">
      <el-pagination
          :page-size="20"
          :pager-count="11"
          layout="prev, pager, next"
          :total="100"
      >
      </el-pagination>

      <el-dialog v-model="dialogVisible" title="Tips" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="汽车配件名称">
            <el-input v-model="form.carPartsName" style="width: 80%"></el-input>
          </el-form-item>
<!--          <el-form-item label="规格">-->
<!--            <el-input v-model="form.standard" style="width: 80%"></el-input>-->
<!--          </el-form-item>-->

          <el-form-item label="规格">
            <el-select v-model="form.standard" class="m-2" placeholder="Select" size="small">
              <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              /></el-select>
          </el-form-item>
          <el-form-item label="配件编号">
            <el-input v-model="form.carNumber" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="进价"  :rules="[
        { required: true, message: 'age is required' },
        { type: 'number', message: 'age must be a number' },
      ]">
            <el-input v-model="form.buy_price" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="卖价" :rules="numberRules">
              <el-input v-model="form.sale_price" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="剩余数量">
            <el-input v-model="form.number" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="缺货数量">
            <el-input v-model="form.sub_number" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="封面">
            <el-upload
                action="http://localhost:9090/files/upload" :on-success="filesUploadSuccess">
              <el-button type="primary">点击上传</el-button>
              <template #tip>
                <div>只能上传jpg/png 文件，且不能超过500kb</div>
              </template>
            </el-upload>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import request from "@/utils/request";
export default {
  name: 'Goods',
  components: {},
  data() {
    return {
      numberRules:[
    { required: true, message: '请输入必要信息' },
    { type: 'number', message: '请输入数字类型' },
     ],
      form: {
        carPartsName:null,
        standard:null,
        carNumber:null,
        buy_price:null,
        sale_price:null,
        number:null,
        sub_number:null,
        cover:null
      },
      options:[
        {
          value: 'S',
          label: 'S',
        },
        {
          value: 'M',
          label: 'M',
        },
        {
          value: 'L',
          label: 'L',
        }
      ],
      dialogVisible: false,
      search: '',
      currentPage: 1,
      total: 10,
      tableData: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    filesUploadSuccess(res){
      console.log(res)
      this.form.cover=res.data
    },
    load() {
      request.get("/goods").then(res => {
        console.log(res)
        this.tableData = res.data
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
      if(this.$refs['upload']){
        this.$refs['upload'].clearFiles()
      }
    },
    save() {
      console.log(11)
      if (this.form.id) {
        //更新
        request.put("/goods/save", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          }
          this.load();
          this.dialogVisible=false;
        })
      } else {
        console.log("更新")
        request.post("/goods/save", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          }
          this.load();
          this.dialogVisible=false;
        })
      }
    },
    handleDelete(row){
      console.log(row.id);
      request.delete("/goods/"+row.id).then(res=>{
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }else {
          this.$message({
            type:"error",
            message:"删除失败"
          })
        }
      });
      this.load();
    },
    handleEdit(row) {
      this.form={
        'carPartsName':row.carPartsName,
        'standard':row.standard,
        'carNumber':row.carNumber,
        'buy_price':row.buy_price,
        'sale_price':row.sale_price,
        'number':row.number,
        'sub_number':row.sub_number,
        'cover':row.cover
      }
      // console.log(row)
      // this.form = JSON.parse(JSON.stringify(row))
      // console.log(this.form)
      this.dialogVisible = true
      this.$nextTick(()=>{
        this.$refs['upload'].clearFiles()
      })

    }
  }

}

</script>
